package im.route.api.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);

    public static <T> T convertJsonStringToObj(String jsonString, Class<T> target) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(jsonString, target);
        } catch (JsonProcessingException e) {
            LOGGER.error("Can't not convert json string to object {}", target.getSimpleName(), e);
            return null;
        }
    }

    public static String convertObjToJsonString(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            LOGGER.error("Can't not convert object to json string.", e);
            return "";
        }
    }
}
